﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="BuySell.aspx.cs" Inherits="BuySell" EnableViewState="true"%>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
    <style type="text/css">

        .style86
    {
        text-align: center;
        background-color: #FFFFFF;
        height: 46px;
            width: 200px;
        }
    td {
        background-color: #FFFFFF;
        background: #CCFFCC;
        font-family: Arvo, serif;
    }
    .style85
    {
        text-align: center;
        background-color: #FFFFFF;
        height: 170px;
    }
    th {
        background-color: #FFFFFF;
        background: #CCCCCC;
        font-family: Arvo, serif;
    }
        #Text1
        {
            width: 32px;
        }
        #Text2
        {
            width: 32px;
        }
        #Text3
        {
            width: 32px;
        }
        #Text4
        {
            width: 32px;
        }
        #Text7
        {
            width: 32px;
        }
        #Text5
        {
            width: 32px;
        }
        #Text8
        {
            width: 32px;
        }
        #Text6
        {
            width: 32px;
        }
        #Text5
        {
            width: 32px;
        }
        #Text6
        {
            width: 32px;
        }
        #Text7
        {
            width: 32px;
        }
        #Text8
        {
            width: 32px;
        }
        #Text8
        {
            width: 32px;
        }
        #Text9
        {
            width: 32px;
        }
        #Text9
        {
            width: 32px;
        }
        .style157
        {
            height: 205px;
            background-color: #FFFFFF;
            text-align: center;
            top: 20px;
            width: 200px;
        }
        #Text10
        {
            width: 52px;
        }
        #Text11
        {
            width: 52px;
        }
        #Text12
        {
            width: 52px;
        }
        #Text13
        {
            width: 52px;
        }
        #Text14
        {
            width: 52px;
        }
        #Text15
        {
            width: 52px;
        }
        #Text16
        {
            width: 52px;
        }
        #Text17
        {
            width: 52px;
        }
        .style196
        {
            text-align: left;
            background-color: #FFFFFF;
            height: 46px;
            width: 800px;
        }
        .style198
        {
            width: 220px;
            background-color: #FFFFCC;
            height: 23px;
        }
        .style199
        {
            width: 61px;
            color: #FFFFFF;
            font-weight: 700;
            background-color: Navy;
            height: 23px;
        }
        .style200
        {
            color: #FF0000;
            width: 220px;
            height: 26px;
            background-color: #FFFFCC;
        }
        .style203
        {
            width: 61px;
            color: #FFFFFF;
            height: 26px;
            font-weight: 700;
            background-color: Navy;
        }
        .style226
        {
            width: 61px;
            color: #FFFFFF;
            height: 24px;
            font-weight: 700;
            background-color:Navy;
        }
        .style227
        {
            width: 220px;
            height: 24px;
            background-color: #FFFFCC;
        }
        .style233
        {
            width: 204px;
            height: 42px;
        }
        .style246
        {
            width: 98px;
            height: 42px;
        }
        .style247
        {
            height: 42px;
            width: 37px;
        }
        .style251
        {
            width: 460px;
            height: 84px;
        }
        .style252
        {
            height: 42px;
            width: 67px;
        }
        .style256
        {
            height: 34px;
            width: 89px;
        }
        .style259
        {
            width: 155px;
            height: 26px;
        }
        .style260
        {
            width: 312px;
            height: auto;
        }
    .style261
    {
        text-align: center;
        background-color: #FFFFFF;
        height: 30px;
    }
    .style262
    {
        text-align: center;
        background-color: #FFFFFF;
        height: 32px;
    }
        .style266
        {
            height: 42px;
            width: 68px;
        }
        .style267
        {
            height: 42px;
            width: 53px;
        }
        .style268
        {
            text-align: center;
            background-color: #FFFFFF;
            height: 170px;
            width: 800px;
        }
        .style269
        {
            height: 800px;
            width: 900px;
        }
        .style270
        {
            text-align: center;
            background-color: #FFFFFF;
            height: 100%;
            top: auto;
        }
    </style>

    <script src="Scripts/dist/jquery.js" type="text/javascript"></script>
    <script src="Scripts/dist/jquery.min.js" type="text/javascript"></script>
    <script src="Scripts/dist/jquery.jqplot.min.js" type="text/javascript"></script>
    <link href="Scripts/dist/jquery.jqplot.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/dist/plugins/jqplot.highlighter.min.js" type="text/javascript"></script>
    <script src="Scripts/dist/plugins/jqplot.cursor.min.js" type="text/javascript"></script>
    <script src="Scripts/dist/plugins/jqplot.dateAxisRenderer.min.js" type="text/javascript"></script>
    <script src="Scripts/dist/plugins/jqplot.ohlcRenderer.min.js" type="text/javascript"></script>
    <script src="Scripts/dist/plugins/jqplot.pointLabels.min.js" type="text/javascript"></script>

    <script language="JavaScript">
        var chart;
        var divToBeWorkedOn = 'chart2';
        var shareCompanyDropdown = '#<%= ShareCompanyDropdown.ClientID %>';
        var zoomed = false;
        var zoomedOut = false;

        $(document).ready(function () {

            $(shareCompanyDropdown).change(function() {
                // Get Dropdownlist seleted item text
   

                return false;
            }) //Close $(shareCompanyDropdown).change(function() {
        });  // Close $(document).ready(function () {

        function drawGraph()
        {
             var selected = $(shareCompanyDropdown + " option:selected").val();
             var graph = $('[id*=GraphType_Radio]').find('input:checked').val(); 
             var duration = $('[id*=Duration_Radio]').find('input:checked').val(); 

             $('#' + divToBeWorkedOn).load(showGraph(selected, graph, duration));  
             checkZoom();
             var refreshId = setInterval(function () {
                if(zoomed != true){
                        $('#' + divToBeWorkedOn).load(showGraph(selected, graph, duration));  
                }
             }, 5 * 1000); //Close var refreshId = setInterval(function () {

            if(graph == "Graph" && selected != "0")
            {
                $('#' + divToBeWorkedOn).load(showGraph(selected, startDate, endDate));  
                checkZoom();
            }
            else 
            {
                    $(divToBeWorkedOn).html('');
            }
        }

        function checkZoom() { 
            $.jqplot.postDrawHooks.push(zoomHandler);
        }

        function zoomHandler() {

           var c = this.plugins.cursor;

           if(c._zoom.zooming) {
                zoomed = true;
                zoomedOut = true;
           } else {
              if (zoomedOut == true)
              {
                var selected = $(shareCompanyDropdown + " option:selected").val();
                var graph = $('[id*=GraphType_Radio]').find('input:checked').val(); 
                var duration = $('[id*=Duration_Radio]').find('input:checked').val(); 
                $('#' + divToBeWorkedOn).load(showGraph(selected, graph, duration));
                zoomedOut = false;
              } 
              zoomed = false;                  
           }
        }

        function showGraph(selected, graph, duration) { 
            var gameID = <%= Session["GameID"].ToString().ToLower() %>;
            var webMethod = 'WebService.asmx/CheckStock';
            var parameters = "{'gameID':'" + gameID + "'}";
            var currentDate = $('#Hidden1').val();

            $.ajax({
                type: "POST",
                url: webMethod,
                data: parameters,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                var change = response.d;

                if(change != currentDate)
                {
                    $('#Hidden1').val(change);
                    webMethod = 'WebService.asmx/DisplayStock';
                    parameters = "{'shareSymbol':'" + selected + "', 'gameID':'" + gameID + "', 'graph':'" + graph + "', 'duration':'" + duration + "'}";
                    var ohlc = [];
                    var line = [];
                    var minDate = '';
                    var maxDate = '';
                    var interval = '1 day';

                    if(duration == 'Monthly')
                        interval = '1 week';

                    else if(duration == 'Yearly')
                        interval = '1 month';

                    $.ajax({
                        type: "POST",
                        url: webMethod,
                        data: parameters,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
               
                            var stocks = response.d;
                            if(stocks != null)
                            {
                                for (var i = 0; i < stocks.length; ++i) {
                                    var stock = stocks[i];

                                    if(i == 0)
                                    {
                                        minDate = stock.Date;
                                        var actualDate = new Date(minDate); // convert to actual date
                                        var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()-1);
                                        minDate = newDate;
                                    }

                                    if((i + 1) == stocks.length)
                                    {
                                        $('#lblRates').html('');
                                        $('#<%= lblRates.ClientID %>').text(stock.Close);
                                    }

                                    if ((i + 1) == stocks.length){
                                        maxDate = stock.Date;
                                        var actualDate = new Date(maxDate); // convert to actual date
                                        var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+1);
                                        maxDate = newDate;
                                    }

                                    var row = [stock.Date, stock.Open, stock.High, stock.Low, stock.Close];
                                    var row2 = [stock.Date, stock.Close];
                                    ohlc.push(row);
                                    line.push(row2)
                                } //Close For loop

                                //Clearing Memory
                                if (chart != null) 
                                {
                                    $('#' + divToBeWorkedOn + ' *').unbind(); // iexplorer
                                    $(divToBeWorkedOn).html('');
                                    $(divToBeWorkedOn).remove();
                                    chart.destroy();
                                    CollectGarbage();
                                }

                                if(graph == 'Candle Stick')
                                {
                                    if (ohlc.length > 0) {
                                        chart = $.jqplot(divToBeWorkedOn,[ohlc],{
                                        seriesDefaults:{
                                            yaxis:'y2axis'
                                        },
                                        axes: {
                                            xaxis: {
                                            label: "Date",
                                            renderer:$.jqplot.DateAxisRenderer,
                                            tickOptions:{formatString:'%b %e'}, 
                                            min: minDate,
                                            max: maxDate,
                                            tickInterval: interval,
                                            },
                                            y2axis: {
                                            label: "Share Price",
                                            tickOptions:{formatString:'%.2f'}
                                            }
                                        },
                                        // To make a candle stick chart, set the "candleStick" option to true.
                                        series: [
                                            {
                                            renderer:$.jqplot.OHLCRenderer, 
                                            rendererOptions:{ candleStick:true }
                                            }
                                        ],
                                        highlighter: {
                                            show: true,
                                            size: 40,
                                            showMarker:false,
                                            tooltipAxes: 'xy',
                                            yvalues: 4,
                                    
                                    
                                            formatString:'<table class="jqplot-highlighter"> \
                                            <tr><td><FONT SIZE="2">Date:</td><td><FONT SIZE="2">%s</td></tr> \
                                            <tr><td><FONT SIZE="2">Open:</td><td><FONT SIZE="2">%s</td></tr> \
                                            <tr><td><FONT SIZE="2">High:</td><td><FONT SIZE="2">%s</td></tr> \
                                            <tr><td><FONT SIZE="2">Low:</td><td><FONT SIZE="2">%s</td></tr> \
                                            <tr><td><FONT SIZE="2">Close:</td><td><FONT SIZE="2">%s</td></tr></table>'
                                    
                                            /*
                                            formatString:'<table class="jqplot-highlighter" > \
                                            <tr><td><FONT SIZE="2">Date: </td><td><FONT SIZE="2">%s</td></tr> \
                                            <tr><td><FONT SIZE="2">Open: </td><td><FONT SIZE="2">%s</td></tr> \</table>'
                                            */
                                        },
                                        cursor: {
                                        show: true,
                                        zoom: true, 
                                        tooltipLocation: 'sw'
                                        }
                                    }); //Close var plot2 = $.jqplot('chart2',[ohlc],{
                                 }
                            }

                            else if(graph == 'Line')
                            {
                                if (line.length > 0) {
                                    chart = $.jqplot(divToBeWorkedOn, [line], {
                                    title: selected,
                                    axes: {
                                        xaxis: {
                                            renderer: $.jqplot.DateAxisRenderer,
                                            tickOptions: {
                                                formatString: '%b&nbsp;%#d'
                                            }
                                        },
                                        yaxis: {
                                            tickOptions: {
                                                formatString: '$%.2f'
                                            }
                                        }
                                    },
                                    cursor: {
                                        show: true,
                                        zoom: true,
                                        tooltipLocation: 'sw'
                                    },
                                    highlighter: {
                                        show: true
                                    },
                                    seriesDefaults: {
                                        showMarker:true,
                                        pointLabels: {
                                        show: false,
                                        edgeTolerance: 5
                                    }},
                                    });

                                    chart.replot();  
                                }
                        
                              }
                        }
                    }, //Close Success
                    error: function (err) {
                        $(divToBeWorkedOn).html("Unavailable");
                    }
                }); //Close $.ajax({ 

                var userName = '<%= Session["UserName"].ToString().ToLower() %>';
                //var listview = '#<%= GridView_UserExistingStock.ClientID %>';
                 webMethod = 'WebService.asmx/DisplayOwnStock';
                $.ajax({
           
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "WebService.asmx/DisplayOwnStock",
                    data: "{'UserName':'" + userName + "'}",
                    dataType: "json",
                    success: function(response) {
                        var stocks = response.d;
                        if(stocks != null)
                        {
                           for (var i = 0; i < stocks.length; ++i) {
                                    var stock = stocks[i];
                            
                                    //$("#GridView_UserExistingStock").append("<tr><td>" + stock.Name + "</td><td>" + stock.Open + "</td></tr>");
                                    $("#NamesGridView").append("<tr><td>" + stock.Name + "</td><td>" + stock.Open + "</td></tr>");
                           }
                        }
                    },
                    error: function(err) {
                        var test = err.toLocaleString();
                    }
                 });
              }
           }
         });
    }

    </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <table align="center" 
        
    style="border: thin solid #000000; background-color: #FFFFFF; " 
    frame="border" class="style269">
        <tr>
            <td align="justify" class="style196" height="auto" 
                style="border: thin solid #000000" valign="top" colspan="2">
                <asp:Label ID="Label1" runat="server" Text="Available Stocks"></asp:Label>
                <br />
                <div style="width: 100%; height: auto" align="left">
                    <table class="style251" frame="above" width="100%" style="width: 100%">
                        <tr>
                            <td align="center" class="style233" colspan="2" bgcolor="White">
        <asp:DropDownList ID="ShareCompanyDropdown" runat="server" 
            DataSourceID="ShareCompanyNameDataSoruce" DataTextField="shareName" 
            DataValueField="shareSymbol" Width="169px" onselectedindexchanged="DropDownList_AvailableStock_SelectedIndexChanged" 
            AutoPostBack="True" AppendDataBoundItems="True"> <asp:ListItem Value=""> Please Select </asp:ListItem>
        </asp:DropDownList>
                                <br />
                            <asp:Label ID="tooltip_label" runat="server" ForeColor="Red" 
                                Text="** Stock you own **"></asp:Label>
                            </td>
                            <td align="center" class="style247" bgcolor="White">
        <asp:Button ID="btn_BuyAvailable" runat="server" 
            onclick="btn_BuyAvailable_Click" Text="Buy" />
                            </td>
                            <td align="center" class="style252" bgcolor="White">
                                QTY</td>
                            <td align="center" class="style266" bgcolor="White">
                                T/P</td>
                            <td align="center" class="style266" colspan="0" bgcolor="White">
                                S/L</td>
                        </tr>
                        <tr align="center">
                            <td align="center" class="style267" bgcolor="White">
                                Rates :</td>
                            <td align="center" class="style246" bgcolor="White">
                                <asp:Label ID="lblRates" runat="server"></asp:Label>
                            </td>
                            <td class="style247" bgcolor="White">
                            </td>
                            <td class="style252" rowspan="0" bgcolor="White">
        <asp:TextBox ID="txtBuyAvailShareQuantity" runat="server" 
            Width="45px"></asp:TextBox>
                            </td>
                            <td class="style266" rowspan="0" bgcolor="White">
                                <asp:TextBox ID="txtBuyTP" runat="server" Width="67px"></asp:TextBox>
                            </td>
                            <td class="style266" colspan="0" bgcolor="White">
                                <asp:TextBox ID="txtBuySL" runat="server" Width="67px"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                    <br />




<asp:SqlDataSource ID="ShareCompanyNameDataSoruce" runat="server" 
        ConnectionString="<%$ ConnectionStrings:stockmarketConnectionString %>" 
        SelectCommand="RetrieveShareCompanyName" 
    SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:SessionParameter Name="UserName" SessionField="UserName" Type="String" />
        <asp:SessionParameter Name="GameID" SessionField="GameID" Type="Int32" 
            DefaultValue="0" />
    </SelectParameters>
    </asp:SqlDataSource>
                    <input id="Hidden1" type="hidden" value=""/>
                </div>
            </td>
            <td align="center" class="style86" style="border: thin solid #000000" 
                rowspan="2" width="100%">
                <table align="center" 
                    style="width: 60%; height: 79px; color: #000000; background-color: #FFFFFF;">
                    <tr>
                        <td align="center" class="style226">
                            Cash</td>
                        <td align="center" class="style227">
                            <asp:Label ID="lblCashOnHand" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" class="style199">
                            Networth</td>
                        <td align="center" class="style198">
                            <asp:Label ID="lblTotalNetworth" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" class="style203">
                            P/L</td>
                        <td align="center" class="style200">
                            <asp:Label ID="lblFloatingPL" runat="server"></asp:Label>
                        </td>
                    </tr>
                </table>
                <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
                <asp:Timer ID="Timer1" runat="server" Interval="5000" Enabled="False">
                </asp:Timer>
            </td>
        </tr>
        <tr>
            <td align="left" class="style196" height="auto" 
                style="border: thin solid #000000" valign="top">
                    <asp:Label ID="GraphType_Label" runat="server" ForeColor="Black" 
                        Text="Please Select Graph Type:"></asp:Label>
                    <br />
                    <asp:RadioButtonList ID="GraphType_Radio" runat="server" AutoPostBack="True" 
                        onselectedindexchanged="GraphType_Radio_SelectedIndexChanged" 
                        RepeatDirection="Horizontal" Width="100%">
                        <asp:ListItem Selected="True">Candle Stick</asp:ListItem>
                        <asp:ListItem>Line</asp:ListItem>
                    </asp:RadioButtonList>
            </td>
            <td align="left" class="style196" height="auto" 
                style="border: thin solid #000000" valign="top">
                    <asp:Label ID="DurationType_Label" runat="server" ForeColor="Black" 
                        Text="Please Select Duration:"></asp:Label>
                    <br />
                    <asp:RadioButtonList ID="Duration_Radio" runat="server" AutoPostBack="True" 
                        onselectedindexchanged="Duration_Radio_SelectedIndexChanged" 
                        RepeatDirection="Horizontal" Width="100%">
                        <asp:ListItem Selected="True">Weekly</asp:ListItem>
                        <asp:ListItem>Monthly</asp:ListItem>
                        <asp:ListItem>Yearly</asp:ListItem>
                    </asp:RadioButtonList>




            </td>
        </tr>
        <tr>
            <td align="justify" class="style268" style="border: thin solid #000000" 
                colspan="2">

    <asp:Label ID="zoom_label" runat="server" ForeColor="#FF3300" 
        Text="You can zoom in by drag and drop. Zoom out by double left click"></asp:Label>

                <div id="chart2" style="height:400px;width:100%; "></div>
<div id="Div1" style="height:20px; width:auto; "></div></td>
            <td bgcolor="White" class="style157" align="center" 
                style="border: thin solid #000000; " valign="top" width="100%">
    
                            <asp:Label ID="tooltip_label0" runat="server" ForeColor="Red" 
                                Text="** Stock you own **"></asp:Label>
    
                <br />
                <div style="width: auto; height: auto">
                    <table class="style260" style="top: auto" align="center">
                        <tr>
                            <td class="style256" colspan="2" width="100%">

                                <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                                    <ContentTemplate>
                                        <asp:GridView ID="GridView_UserExistingStock" runat="server" 
                                            AutoGenerateColumns="False" CellPadding="4" 
                                            DataSourceID="SqlDataSource_UserExisitingStock" ForeColor="#333333" 
                                            GridLines="None" Width="100%">
                                            <AlternatingRowStyle BackColor="White" />
                                            <Columns>
                                                <asp:BoundField DataField="UserName" HeaderText="UserName" 
                                                    SortExpression="UserName" Visible="False" />
                                                <asp:BoundField DataField="ShareName" HeaderText="Symbol" 
                                                    SortExpression="ShareName" />
                                                <asp:BoundField DataField="CloseValue" HeaderText="Price" 
                                                    SortExpression="CloseValue" />
                                                <asp:ButtonField ButtonType="Button" CommandName="BuyStock" HeaderText="Action" 
                                                    Text="Buy" />
                                            </Columns>
                                            <EditRowStyle BackColor="#2461BF" />
                                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                                            <RowStyle BackColor="#EFF3FB" />
                                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                            <SortedAscendingCellStyle BackColor="#F5F7FB" />
                                            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                                            <SortedDescendingCellStyle BackColor="#E9EBEF" />
                                            <SortedDescendingHeaderStyle BackColor="#4870BE" />
                                        </asp:GridView>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" class="style259" width="100%">
                                Quantity :</td>
                            <td align="center" class="style259" width="100%">
                            <asp:TextBox ID="txt_QtyUserExisitingStock" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                        </table>
                    <br />
                </div>
    
        <asp:SqlDataSource ID="SqlDataSource_UserExisitingStock" runat="server" 
            ConnectionString="<%$ ConnectionStrings:stockmarketConnectionString %>" 

                    
                    
                    
                    
                    SelectCommand="SELECT DISTINCT UserCurrentStock.UserName, UserCurrentStock.ShareName, ShareSimulation.CloseValue FROM UserCurrentStock INNER JOIN Game ON UserCurrentStock.GameID = Game.GameID INNER JOIN ShareSimulation ON Game.GameID = ShareSimulation.GameID AND UserCurrentStock.ShareName = ShareSimulation.ShareSymbol AND Game.GameElapsedDate = ShareSimulation.HistoryDate WHERE (UserCurrentStock.UserName = @UserName) AND (UserCurrentStock.ShareQuantity &lt;&gt; 0)">
            <SelectParameters>
                <asp:SessionParameter Name="UserName" SessionField="UserName" />
            </SelectParameters>
        </asp:SqlDataSource>

                <br />
                <br />
                     </td>
        </tr>
        <tr>
            <td align="left" class="style261" colspan="3" height="auto">

                Edit TP/SL</td>
        </tr>
        <tr>
            <td align="left" class="style262" colspan="3" height="100%" 
                style="border: thin solid #000000">

        <asp:DropDownList ID="DropDownList_SelectedStock" runat="server" 
            AutoPostBack="True" DataSourceID="SqlDataSource_UserExisitingStock" 
            DataTextField="ShareName" DataValueField="ShareName" AppendDataBoundItems="true" 
                    Width="50%" 
                    onselectedindexchanged="DropDownList_SelectedStock_SelectedIndexChanged"> <asp:ListItem Value=""> Please Select </asp:ListItem>
        </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td align="center" class="style270" colspan="3" 
                style="border: thin solid #000000; " valign="top">

        <asp:GridView ID="GridView_EditTPSL" runat="server" AutoGenerateColumns="False" 
            BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
            CellPadding="3" 
            DataSourceID="SqlDataSource_UserEditTPSL" Width="100%" 
                    DataKeyNames="TransactionID" onrowupdated="GridView_EditTPSL_RowUpdated" 
                    style="margin-bottom: 0px" >
            <Columns>
                <asp:BoundField DataField="UserName" HeaderText="UserName" 
                    SortExpression="UserName" Visible="False" ReadOnly="True" />
                <asp:BoundField DataField="GameID" HeaderText="GameID" SortExpression="GameID" 
                    Visible="False" ReadOnly="True" />
                <asp:BoundField DataField="TransactionID" HeaderText="TransactionID" 
                    ReadOnly="True" SortExpression="TransactionID" />
                <asp:BoundField DataField="ShareName" HeaderText="Symbol" 
                    SortExpression="ShareName" ReadOnly="True" />
                <asp:BoundField DataField="TransactionDate" HeaderText="Transaction Date" 
                    SortExpression="TransactionDate" ReadOnly="True" />
                <asp:BoundField DataField="ShareValue" HeaderText="Bought Price" 
                    SortExpression="ShareValue" ReadOnly="True" />
                <asp:BoundField DataField="ShareQuantity" HeaderText="Qty" 
                    SortExpression="ShareQuantity" ReadOnly="True" />

                <asp:TemplateField HeaderText="T/P" SortExpression="ShareTP">
                    <EditItemTemplate>
                        <asp:TextBox ID="EditShareTP" runat="server" 
                            Text='<%# Bind("ShareTP", "{0}") %>' Width="50px"></asp:TextBox>
                        <asp:CompareValidator ID="CompareValidator1" runat="server" 
                            ErrorMessage="T/P price need to be more than 0" ForeColor="Red" 
                            Operator="GreaterThan" ValueToCompare="0" ControlToValidate="EditShareTP" Type=Double></asp:CompareValidator>
                    </ItemTemplate>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblTP" runat="server" Text='<%# Bind("ShareTP", "{0:N}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="S/L" SortExpression="ShareSL">
                    <EditItemTemplate>
                        <asp:TextBox ID="EditShareSL" runat="server" 
                            Text='<%# Bind("ShareSL", "{0}") %>' Width="50px"></asp:TextBox>
                        <asp:CompareValidator ID="CompareValidator2" runat="server" 
                            ErrorMessage="S/L price need to be more than 0" ForeColor="Red" 
                            Operator="GreaterThan" ValueToCompare=0 ControlToValidate="EditShareSL" Type=Double></asp:CompareValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblSL" runat="server" Text='<%# Bind("ShareSL", "{0:N}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:CommandField ButtonType="Button" ShowEditButton="True" >
                <ControlStyle Width="75px" />
                <ItemStyle Wrap="False" />
                </asp:CommandField>
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#007DBB" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#00547E" />
        </asp:GridView>
                <br />

        <asp:SqlDataSource ID="SqlDataSource_UserEditTPSL" runat="server" 
            ConnectionString="<%$ ConnectionStrings:stockmarketConnectionString %>" 
            SelectCommand="SELECT UserCurrentStock.UserName, UserCurrentStock.GameID, UserCurrentStock.ShareName, CONVERT (VARCHAR, [Transaction].TransactionDate, 101) AS TransactionDate, UserCurrentStock.ShareValue, UserCurrentStock.ShareQuantity, CASE WHEN UserCurrentStock.ShareTP &gt; - 1 THEN UserCurrentStock.ShareTP ELSE 0 END AS ShareTP, CASE WHEN UserCurrentStock.ShareSL &gt; - 1 THEN UserCurrentStock.ShareSL ELSE 0 END AS ShareSL, UserCurrentStock.TransactionID FROM UserCurrentStock INNER JOIN [Transaction] ON UserCurrentStock.TransactionID = [Transaction].TransactionID WHERE (UserCurrentStock.UserName = @UserName) AND (UserCurrentStock.ShareName = @shareName)" 
            
                    UpdateCommand="UPDATE [UserCurrentStock] SET ShareTP = @ShareTP, ShareSL = @ShareSL WHERE (TransactionID = @TransactionID)">
            <SelectParameters>
                <asp:SessionParameter Name="UserName" SessionField="UserName" />
                <asp:ControlParameter ControlID="DropDownList_SelectedStock" DefaultValue="" 
                    Name="shareName" PropertyName="SelectedValue" />
            </SelectParameters>
            <UpdateParameters>
                <asp:ControlParameter ControlID="GridView_EditTPSL" Name="ShareTP" 
                    PropertyName="SelectedValue" Type="Double" />
                <asp:ControlParameter ControlID="GridView_EditTPSL" Name="ShareSL" 
                    PropertyName="SelectedValue" Type="Double" />
                <asp:ControlParameter ControlID="GridView_EditTPSL" Name="TransactionID" 
                    PropertyName="SelectedValue" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
                <br />
            </td>
        </tr>
        <tr>
            <td align="left" class="style85" colspan="3" 
                style="border: thin solid #000000; height: auto; top: auto; " 
                valign="top">

                <br />

                Quantitiy : 
        <asp:TextBox ID="txtSellShareQuantity" runat="server"></asp:TextBox>
                <br />
                <div style="height: 20px; width: 100%;">
                </div>
            </td>
        </tr>
        <tr>
            <td align="center" class="style85" colspan="3" height="auto" 
                style="border: thin solid #000000; height: auto; top: auto;" valign="top">
                    
        <asp:GridView ID="GridView_SellStock" runat="server" 
            AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" 
            BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            DataKeyNames="TransactionID" 
            DataSourceID="SqlDataSource_SellUserExistingStock" Width="100%">
            <Columns>
                <asp:BoundField DataField="TransactionID" HeaderText="TransactionID" 
                    InsertVisible="False" SortExpression="TransactionID" ReadOnly="True" />
                <asp:BoundField DataField="GameID" HeaderText="GameID" 
                    SortExpression="GameID" Visible="False" />
                <asp:BoundField DataField="UserName" HeaderText="UserName" 
                    SortExpression="UserName" Visible="False" />
                <asp:BoundField DataField="ShareName" HeaderText="Symbol" 
                    SortExpression="ShareName" />
                <asp:BoundField DataField="TransactionDate" HeaderText="Transaction Date" 
                    SortExpression="TransactionDate" />
                <asp:BoundField DataField="ShareValue" HeaderText="Bought Price" 
                    SortExpression="ShareValue" />
<asp:BoundField DataField="CloseValue" HeaderText="Current Price" SortExpression="CloseValue"></asp:BoundField>
                <asp:BoundField DataField="ShareQuantity" HeaderText="Qty" 
                    SortExpression="ShareQuantity" />
                <asp:BoundField DataField="TransactionType" HeaderText="Type" 
                    SortExpression="TransactionType" />
                <asp:BoundField DataField="ShareTP" HeaderText="T/P" 
                    SortExpression="ShareTP" />
                <asp:BoundField DataField="ShareSL" HeaderText="S/L" 
                    SortExpression="ShareSL" />
                <asp:BoundField DataField="RelatedTransactionID" 
                    HeaderText="RelatedTransactionID" SortExpression="RelatedTransactionID" 
                    Visible="False" />
                <asp:ButtonField ButtonType="Button" CommandName="SellStock" 
                    HeaderText="Action" Text="Sell" />
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#007DBB" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#00547E" />
        </asp:GridView>
                <br />
        <asp:SqlDataSource ID="SqlDataSource_SellUserExistingStock" runat="server" 
            ConnectionString="<%$ ConnectionStrings:stockmarketConnectionString %>" 
                    
                    
                    
                    
                    SelectCommand="SELECT 
UserCurrentStock.TransactionID, UserCurrentStock.GameID, UserCurrentStock.UserName, 
UserCurrentStock.ShareName, 
CONVERT (VARCHAR, [Transaction].TransactionDate, 101) AS TransactionDate, 
[Transaction].ShareValue, UserCurrentStock.ShareQuantity, [Transaction].TransactionType, 
 CASE 
	WHEN UserCurrentStock.ShareTP &gt; -1
	THEN UserCurrentStock.ShareTP
	ELSE 0
 END AS ShareTP,
 CASE 
	WHEN UserCurrentStock.ShareSL &gt; -1
	THEN UserCurrentStock.ShareSL
	ELSE 0
 END AS ShareSL
 , [Transaction].RelatedTransactionID, 
ShareSimulation.CloseValue 
FROM 
[Transaction] INNER JOIN UserCurrentStock 
ON 
[Transaction].TransactionID = UserCurrentStock.TransactionID INNER JOIN Game 
ON 
[Transaction].GameID = Game.GameID INNER JOIN ShareSimulation 
ON 
[Transaction].GameID = ShareSimulation.GameID AND Game.GameElapsedDate = ShareSimulation.HistoryDate 
AND 
UserCurrentStock.ShareName = ShareSimulation.ShareSymbol 
WHERE 
([Transaction].TransactionType = 'BUY') 
AND (UserCurrentStock.ShareQuantity &gt; 0) AND (UserCurrentStock.UserName = @UserName)">
            <SelectParameters>
                <asp:SessionParameter DefaultValue="" Name="UserName" SessionField="UserName" 
                    Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
            </td>
        </tr>
        </table>
</asp:Content>

